<?xml version="1.0" encoding="UTF-8" ?>
<!--

    Copyright Camunda Services GmbH and/or licensed to Camunda Services GmbH
    under one or more contributor license agreements. See the NOTICE file
    distributed with this work for additional information regarding copyright
    ownership. Camunda licenses this file to you under the Apache License,
    Version 2.0; you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="org.camunda.bpm.engine.impl.persistence.entity.TaskMeterLogEntity">

  <!-- TASK METER INSERT -->

  <insert id="insertTaskMeterLog" parameterType="org.camunda.bpm.engine.impl.persistence.entity.TaskMeterLogEntity">
    insert into ${prefix}ACT_RU_TASK_METER_LOG (ID_, TIMESTAMP_, ASSIGNEE_HASH_)
    values (
    #{id, jdbcType=VARCHAR},
    #{timestamp, jdbcType=TIMESTAMP},
    #{assigneeHash, jdbcType=BIGINT}
    )
  </insert>

  <!-- TASK METER BULK DELTE -->

  <delete id="deleteTaskMeterLogEntriesByTimestamp" parameterType="java.util.Map">
    delete from ${prefix}ACT_RU_TASK_METER_LOG
    <where>
      <if test="timestamp != null">
        and TIMESTAMP_ &lt;= #{timestamp, jdbcType=TIMESTAMP}
      </if>
    </where>
  </delete>
  
  <delete id="deleteTaskMeterLogEntriesByIds" parameterType="string">
    delete from ${prefix}ACT_RU_TASK_METER_LOG
    where
      <bind name="listOfIds" value="list"/>
      <bind name="fieldName" value="'ID_'"/>
      <include refid="org.camunda.bpm.engine.impl.persistence.entity.Commons.applyInForPaginatedCollection"/>
  </delete>
  
  <delete id="deleteTaskMetricsByRemovalTime"
          parameterType="org.camunda.bpm.engine.impl.db.ListQueryParameterObject">
    <bind name="date" value="'TIMESTAMP_'"/>
    <bind name="reportPeriodUnitName" value="'MINUTE'"/>
    delete ${limitBeforeWithoutOffset} from ${prefix}ACT_RU_TASK_METER_LOG
    where TIMESTAMP_ &lt;= #{parameter.removalTime}
    <include refid="andWhereMinuteInDateBetweenSql"/>
    ${limitAfterWithoutOffset}
  </delete>

  <delete id="deleteTaskMetricsByRemovalTime_oracle"
          parameterType="org.camunda.bpm.engine.impl.db.ListQueryParameterObject">
    <bind name="date" value="'TIMESTAMP_'"/>
    <bind name="reportPeriodUnitName" value="'MINUTE'"/>
    delete ${limitBeforeWithoutOffset} from ${prefix}ACT_RU_TASK_METER_LOG
    where TIMESTAMP_ &lt;= #{parameter.removalTime}
    <include refid="andWhereMinuteInDateBetweenSql_oracle"/>
    ${limitAfterWithoutOffset}
  </delete>

  <delete id="deleteTaskMetricsByRemovalTime_postgres_or_db2"
          parameterType="org.camunda.bpm.engine.impl.db.ListQueryParameterObject">
    <bind name="date" value="'TIMESTAMP_'"/>
    <bind name="reportPeriodUnitName" value="'MINUTE'"/>
    delete ${limitBeforeWithoutOffset} from ${prefix}ACT_RU_TASK_METER_LOG
    where ID_ IN
      (SELECT ID_
       FROM ${prefix}ACT_RU_TASK_METER_LOG
       WHERE TIMESTAMP_ &lt;= #{parameter.removalTime} <include refid="andWhereMinuteInDateBetweenSql"/>
       ${limitAfterWithoutOffset})
  </delete>

  <!-- SELECT -->

  <select id="selectUniqueTaskWorkerCount" parameterType="java.util.Map" resultType="long">
    ${countDistinctBeforeStart} RES.ASSIGNEE_HASH_ ${countDistinctBeforeEnd}
    from ${prefix}ACT_RU_TASK_METER_LOG RES
    <where>
      <if test="startTime != null">
        and RES.TIMESTAMP_ &gt;= #{startTime, jdbcType=TIMESTAMP}
      </if>
      <if test="endTime != null">
        and RES.TIMESTAMP_ &lt; #{endTime, jdbcType=TIMESTAMP}
      </if>
    </where>
    ${countDistinctAfterEnd}
  </select>
  
  <!-- Select ids for async history cleanup -->
  <select id="selectTaskMetricIdsForCleanup" parameterType="org.camunda.bpm.engine.impl.db.ListQueryParameterObject" resultType="string">
    <include refid="selectTaskMetricIdsForCleanupSql"/>
    <include refid="andWhereMinuteInDateBetweenSql"/>
    ${orderBy}
    ${limitAfter}
  </select>

  <select id="selectTaskMetricIdsForCleanup_oracle" parameterType="org.camunda.bpm.engine.impl.db.ListQueryParameterObject" resultType="string">
    <include refid="selectTaskMetricIdsForCleanupSql"/>
    <include refid="andWhereMinuteInDateBetweenSql_oracle"/>
    ${orderBy}
    ${limitAfter}
  </select>

  <sql id="selectTaskMetricIdsForCleanupSql">
    <include refid="org.camunda.bpm.engine.impl.persistence.entity.Commons.bindOrderBy"/>
    <bind name="date" value="'RES.TIMESTAMP_'"/>
    <bind name="days" value="parameter.timeToLive"/>
    <bind name="currentTimestamp" value="parameter.currentTimestamp"/>
    <bind name="reportPeriodUnitName" value="'MINUTE'"/>
    ${limitBefore}
    SELECT RES.ID_, RES.TIMESTAMP_
    ${limitBetween}
    FROM ${prefix}ACT_RU_TASK_METER_LOG RES
    WHERE ${dayComparator}
  </sql>

  <sql id="andWhereMinuteInDateBetweenSql">
    <if test="parameter.minuteFrom != null and parameter.minuteTo != null">
      AND ${datepart1}<include refid="org.camunda.bpm.engine.impl.persistence.entity.Commons.periodUnitFunction"/>${datepart2}${date}${datepart3}
      between #{parameter.minuteFrom, jdbcType=INTEGER} and #{parameter.minuteTo, jdbcType=INTEGER}
    </if>
  </sql>

  <sql id="andWhereMinuteInDateBetweenSql_oracle">
    <if test="parameter.minuteFrom != null and parameter.minuteTo != null">
      AND ${datepart1}${date}${datepart2}<include refid="org.camunda.bpm.engine.impl.persistence.entity.Commons.periodUnitFunction"/>${datepart3}
      between #{parameter.minuteFrom, jdbcType=INTEGER} and #{parameter.minuteTo, jdbcType=INTEGER}
    </if>
  </sql>

</mapper>
